from django.db import models

carTypeList = [
    {"carTypeNo": "3651", "carTypeText": "临时车A"},
    {"carTypeNo": "3650", "carTypeText": "临时车B"},
    {"carTypeNo": "3649", "carTypeText": "临时车C"},
    {"carTypeNo": "3648", "carTypeText": "临时车D"},
    {"carTypeNo": "3652", "carTypeText": "月租车A"},
    {"carTypeNo": "3653", "carTypeText": "月租车B"},
    {"carTypeNo": "3654", "carTypeText": "月租车C"},
    {"carTypeNo": "3655", "carTypeText": "月租车D"},
    {"carTypeNo": "3661", "carTypeText": "月租车E"},
    {"carTypeNo": "3662", "carTypeText": "月租车F"},
    {"carTypeNo": "3663", "carTypeText": "月租车G"},
    {"carTypeNo": "3664", "carTypeText": "月租车H"},
    {"carTypeNo": "3656", "carTypeText": "免费车"},
    {"carTypeNo": "3657", "carTypeText": "储值车"},
    {"carTypeNo": "3660", "carTypeText": "超时收费"},
]


# 访问车辆管理
class Car(models.Model):
    plate = models.CharField("车牌号码", max_length=16, unique=True)
    STATUS_CHOISE = (
        (0, "新创建"),
        (1, "申请中"),
        (2, "待入场"),
        (3, "已入场"),
        (4, "已出场"),
    )
    status = models.IntegerField("可用状态", choices=STATUS_CHOISE, default=0)
    visitor_no = models.CharField("访客车辆ID", max_length=50, null=True, blank=True)
    begin_time = models.CharField("有效期开始", max_length=50, null=True, blank=True)
    end_time = models.CharField("有效期结束", max_length=50, null=True, blank=True)
    enter_time = models.CharField("车辆入场时间", max_length=50, null=True, blank=True)
    enter_gate_name = models.CharField("车辆入场车道", max_length=50, null=True, blank=True)
    out_time = models.CharField("车辆出场时间", max_length=50, null=True, blank=True)
    out_gate_name = models.CharField("车辆出场车道", max_length=50, null=True, blank=True)
    created = models.DateTimeField("创建时间", auto_now_add=True, null=True, blank=True)
    updated = models.DateTimeField("更新时间", auto_now=True, null=True, blank=True)

    def __str__(self):
        return f"访客车辆-{self.plate}"

    class Meta:
        verbose_name = verbose_name_plural = "访客车辆"


class MthCar(models.Model):
    carNo = models.CharField("车牌号码", max_length=20, unique=True)
    carTypeNo = models.CharField("车辆类型编码", max_length=4)
    carTypeText = models.CharField("车辆类型编码", max_length=4, null=True, blank=True)
    beginTime = models.CharField("车位有效期开始时间", max_length=20)
    endTime = models.CharField("车位有效期结束时间", max_length=20)
    iusseTime = models.CharField("发行时间", max_length=20)
    userNo = models.CharField("人员编号", max_length=6, null=True, blank=True)
    userName = models.CharField("车主姓名", max_length=20)
    phone = models.CharField("车主手机", max_length=11)
    homeAddress = models.CharField("车主地址", max_length=200)
    created = models.DateTimeField("创建时间", auto_now_add=True, null=True, blank=True)
    updated = models.DateTimeField("更新时间", auto_now=True, null=True, blank=True)

    def __str__(self):
        return f"月租车辆-{self.carNo}"

    def save(self, *args, **kwargs):
        carTypeNo = self.carTypeNo
        carTypeText = "未知"
        for carType in carTypeList:
            if carType["carTypeNo"] == carTypeNo:
                carTypeText = carType["carTypeText"]
                break
        self.carTypeText = carTypeText
        super(MthCar, self).save(*args, **kwargs)

    class Meta:
        verbose_name = verbose_name_plural = "月租车辆"
